<?php

class ApiController extends Zend_Controller_Action
{
    function init()
    {
        $this->db = Zend_Registry::get("db");
    }
    
    function preDispatch()
    {
        
    }
    
	/**
     * $arr = array(
     *              'verify' => array(
     *                                  'userid' =>
     *                                  'pwd' =>
     *                                  'token' =>
	 *				'data' => array(
	 *									'function_name' => 
	 *									'params' => array()
	 *			)
     *      )
     * )
     */
    function port1Action()
    {
        $postStr = file_get_contents("php://input");
		
        $init = new Sys_Init();
        $result = $init->GetJsonVerify($postStr);
		
        if($result[0]) //passed
        {
            $identity_verify = $init->IdentityVerify($result[1]);
			
			if($identity_verify[0])
			{
				//Passed
				$mod_api = new Api_Port1();
				
				if($result[1]['data']['function_name'])
				{
					//$api_feedback must be an array
					$api_feedback = call_user_func_array(array($mod_api, $result[1]['data']['function_name']), array($result[1]['data']['params']));
					
					//execute
					echo $init->FinalDecolation('port1', $api_feedback, $result[1]['verify']['userid'], $result[1]['verify']['token']);
				}else{
					echo $init->ErrorJsonDecolation("1003");
				}
			}else{
				echo $init->ErrorJsonDecolation($identity_verify[1]);
			}
        }else{ //rejected
            echo $init->ErrorJsonDecolation($result[1]);
        }
		
    	die;
    }
}

/**
 * Error Code
 * 
 * 1001 Incorrect Json Format
 * 1002 Not An Array
 * 1003 Required Info Missed
 * 1004 Identity Verification Rejection
 * 
 */